package com.kaluli.project.repository.impl;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.kaluli.project.domain.entity.KllMq;
import com.kaluli.project.domain.entity.KllOrderSynApi;
import com.kaluli.project.domain.repository.KllOrderSynApiRepository;
import com.kaluli.system.repository.AbstractRepositorySupport;
@Repository
public class KllOrderSynApiRepositoryImpl extends AbstractRepositorySupport<KllOrderSynApi> implements KllOrderSynApiRepository{

	@Override
	protected Class<KllOrderSynApi> getEntityClass() {
		// TODO Auto-generated method stub
		return KllOrderSynApi.class;
	}

	
	@Override
	public KllOrderSynApi findByOrderNumber(String orderNumber) {
		StringBuffer hql=new StringBuffer(" from KllOrderSynApi t where t.orderNumber=:order_number");
		Map<String,Object> params=new HashMap<String, Object>();
		params.put("order_number", orderNumber);
		List<KllOrderSynApi> list =  this.findHqlForMap(hql.toString(), params);
		KllOrderSynApi kllOrderSynApi = null;
		if(list!=null&list.size()>0){
			kllOrderSynApi = list.get(0);
		}
		return kllOrderSynApi;
	}
	
	public KllOrderSynApi findByEdiOrderno(String ediOrderno) {
		StringBuffer hql=new StringBuffer(" from KllOrderSynApi t where t.ediOrderno=:ediOrderno");
		Map<String,Object> params=new HashMap<String, Object>();
		params.put("ediOrderno", ediOrderno);
		List<KllOrderSynApi> list =  this.findHqlForMap(hql.toString(), params);
		KllOrderSynApi kllOrderSynApi = null;
		if(list!=null&list.size()>0){
			kllOrderSynApi = list.get(0);
		}
		return kllOrderSynApi;
	}
	
	public List<KllOrderSynApi> findEDISynApiList(String wareType) {
		StringBuffer hql=new StringBuffer(" from KllOrderSynApi t where t.wareType =:wareType and t.sendNr=2");
		Map<String,Object> params=new HashMap<String, Object>();
		params.put("wareType", wareType);
		List<KllOrderSynApi> list =  this.findHqlForMap(hql.toString(), params);
		return list;
	}
	
	
	
	public List<KllOrderSynApi> findSynApiList(String orderNumber) {
		StringBuffer hql=new StringBuffer(" from KllOrderSynApi t where t.orderNumber=:order_number");
		Map<String,Object> params=new HashMap<String, Object>();
		params.put("order_number", orderNumber);
		List<KllOrderSynApi> list =  this.findHqlForMap(hql.toString(), params);
		return list;
	}
	
	public List<KllOrderSynApi> findSynApiListExmail(String startDate,String endDate) {
		StringBuffer hql=new StringBuffer(" from KllOrderSynApi t where t.sendHg=3 or t.sendGj=3 or  t.sendAlipay=3 or t.sendAlipayGj=3 or t.sendZz=3 or t.sendNr=3");
		Map<String,Object> params=new HashMap<String, Object>();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		try {
//			params.put("startDate", sf.parse(startDate));
//			params.put("endDate", sf.parse(endDate));
		} catch (Exception e) {
		}
		List<KllOrderSynApi> list =  this.findHqlForMap(hql.toString(), params);
		return list;
	}
	
	public List<KllOrderSynApi> findSynApiHGList() {
		StringBuffer hql=new StringBuffer(" from KllOrderSynApi t where t.sendHg=1 ");
		Map<String,Object> params=new HashMap<String, Object>();
		List<KllOrderSynApi> list =  this.findHqlForMap(hql.toString(), params);
		return list;
	}
	
	public List<KllOrderSynApi> findSynApiNrList() {
		StringBuffer hql=new StringBuffer(" from KllOrderSynApi t where t.sendNr=3 ");
		Map<String,Object> params=new HashMap<String, Object>();
		List<KllOrderSynApi> list =  this.findHqlForMap(hql.toString(), params);
		return list;
	}


	@Override
	public List<KllOrderSynApi> listNotPushOrder() {
		StringBuffer hql=new StringBuffer(" from KllOrderSynApi t where (t.sendZz=:sendZz or t.sendZz=:sendZz1) or (t.sendGj=:sendGj or t.sendGj=:sendGj1) or (t.sendHg=:sendHg or t.sendHg=:sendHg1)");
		hql.append(" or (t.sendAlipay=:sendAlipay or t.sendAlipay=:sendAlipay1) or (t.sendAlipayGj=:sendAlipayGj or t.sendAlipayGj=:sendAlipayGj1)  or (t.sendWeixin=:sendWeixin or t.sendWeixin=:sendWeixin1) ");
		hql.append("  or (t.sendNr=:sendNr or t.sendNr=:sendNr1)  ");
		Map<String,Object> params=new HashMap<String, Object>();
		params.put("sendGj", 0);
		params.put("sendGj1", 1);
		params.put("sendZz", 0);
		params.put("sendZz1", 1);
		params.put("sendHg", 0);
		params.put("sendHg1", 1);
		params.put("sendAlipay", 0);
		params.put("sendAlipay1", 1);
		params.put("sendAlipayGj", 0);
		params.put("sendAlipayGj1", 1);
		params.put("sendWeixin", 0);
		params.put("sendWeixin1", 1);
		params.put("sendNr", 0);
		params.put("sendNr1", 1);
		List<KllOrderSynApi> list =  this.findHqlForMap(hql.toString(), params);
		return list;
	}


	@Override
	public void updateSynStatus(String orderNumber, String msgchannel) {
		StringBuffer hql = null;
	/**
	 * 卓志接口是同步返回 直接置为2成功
	 * 国检接口是异步返回 所以设置中间状态未1
	 */
		if(msgchannel.equals("ZZ")){
			hql = new StringBuffer("update KllOrderSynApi t set t.sendZz=2,t.sendZzDate=? where t.orderNumber=? ");
		
		}else if(msgchannel.equals("GJ")){
			hql = new StringBuffer("update KllOrderSynApi t set t.sendGj=1,t.sendGjDate=? where t.orderNumber=? ");
			
		}else if(msgchannel.equals("HG")){
			hql = new StringBuffer("update KllOrderSynApi t set t.sendHg=1,t.sendHgDate=? where t.orderNumber=? ");
			
		}else if(msgchannel.equals("AlipayHg")){
			hql = new StringBuffer("update KllOrderSynApi t set t.sendAlipay=2,t.sendAlipayDate=? where t.orderNumber=? ");
			
		}else if(msgchannel.equals("AlipayGj")){
			hql = new StringBuffer("update KllOrderSynApi t set t.sendAlipayGj=2,t.sendAlipayGjDate=? where t.orderNumber=? ");
			
		}else if(msgchannel.equals("ZZ_status")){
			hql = new StringBuffer("update KllOrderSynApi t set t.sendZz=3,t.sendZzDate=? where t.orderNumber=?  ");
			
		}else if(msgchannel.equals("AlipayHg_error")){
			hql = new StringBuffer("update KllOrderSynApi t set t.sendAlipay=3,t.sendAlipayDate=? where t.orderNumber=?  ");
			
		}else if(msgchannel.equals("AlipayGj_error")){
			hql = new StringBuffer("update KllOrderSynApi t set t.sendAlipayGj=3,t.sendAlipayGjDate=? where t.orderNumber=?  ");
			
		}else if(msgchannel.equals("Weixin")){
			hql = new StringBuffer("update KllOrderSynApi t set t.sendWeixin=2,t.sendWeixinDate=? where t.orderNumber=?  ");
			
		}else if(msgchannel.equals("Weixin_error")){
			hql = new StringBuffer("update KllOrderSynApi t set t.sendWeixin=3,t.sendWeixinDate=? where t.orderNumber=?  ");
			
		}else if(msgchannel.equals("EDI_ERROR")){
			hql = new StringBuffer("update KllOrderSynApi t set t.sendNr=3,t.sendNrDate=? where t.orderNumber=?  ");
			
		}else if(msgchannel.equals("EDI")){
			hql = new StringBuffer("update KllOrderSynApi t set t.sendNr=2,t.sendNrDate=? where t.orderNumber=?  ");
			
		}else if(msgchannel.equals("EDI_WARE")){
			hql = new StringBuffer("update KllOrderSynApi t set t.sendNr=5,t.sendNrDate=? where t.orderNumber=?  ");
			
		}else if(msgchannel.equals("EDI_END")){
			hql = new StringBuffer("update KllOrderSynApi t set t.sendNr=6,t.sendNrDate=? where t.orderNumber=?  ");

		}
		
		
		Object[] params = {new Timestamp(System.currentTimeMillis()),orderNumber};
		int flag =  this.executeHql(hql.toString(), params);
	}


	@Override
	public void updateNSynGjStatus(String orderNumber) {
		StringBuffer 	hql = new StringBuffer("update KllOrderSynApi t set t.sendGj=2,t.sendGjDate=? where t.orderNumber=? ");
		Object[] params = {new Timestamp(System.currentTimeMillis()),orderNumber};
		this.executeHql(hql.toString(), params);
	}
	
	public void updateEDIOrderNo(String orderNumber,String orderNo) {
		StringBuffer 	hql = new StringBuffer("update KllOrderSynApi t set t.ediOrderno=? where t.orderNumber=? ");
		Object[] params = {orderNo,orderNumber};
		this.executeHql(hql.toString(), params);
	}
	
	public void updateNSynHgStatus(String orderNumber) {
		StringBuffer 	hql = new StringBuffer("update KllOrderSynApi t set t.sendHg=2,t.sendHgDate=? where t.orderNumber=? ");
		Object[] params = {new Timestamp(System.currentTimeMillis()),orderNumber};
		this.executeHql(hql.toString(), params);
	}
	
	public void updateNSynGjEnd(String orderNumber) {
		StringBuffer 	hql = new StringBuffer("update KllOrderSynApi t set t.sendGj=3,t.sendGjDate=? where t.orderNumber=? ");
		Object[] params = {new Timestamp(System.currentTimeMillis()),orderNumber};
		this.executeHql(hql.toString(), params);
	}
	
	public void updateNSynHgEnd(String orderNumber) {
		StringBuffer 	hql = new StringBuffer("update KllOrderSynApi t set t.sendHg=3,t.sendHgDate=? where t.orderNumber=? ");
		Object[] params = {new Timestamp(System.currentTimeMillis()),orderNumber};
		this.executeHql(hql.toString(), params);
	}
	
	public void updateNSynStart(String orderNumber) {
		StringBuffer 	hql = new StringBuffer("update KllOrderSynApi t set t.sendGj=1,t.sendGjDate=?,t.sendHg=0,t.sendHgDate=? where t.orderNumber=? ");
		Object[] params = {new Timestamp(System.currentTimeMillis()),new Timestamp(System.currentTimeMillis()),orderNumber};
		this.executeHql(hql.toString(), params);
	}
	
	public void updateSendNr(String orderNumber) {
		StringBuffer 	hql = new StringBuffer("update KllOrderSynApi t set t.sendNr=0,t.sendNrDate=? where t.orderNumber=? ");
		Object[] params = {new Timestamp(System.currentTimeMillis()),orderNumber};
		this.executeHql(hql.toString(), params);
	}
	
	
	public void cancelResult(String ediOrderno) {
		StringBuffer 	hql = new StringBuffer("update KllOrderSynApi t set t.sendNr=4,t.sendNrDate=? where t.ediOrderno=? ");
		Object[] params = {new Timestamp(System.currentTimeMillis()),ediOrderno};
		this.executeHql(hql.toString(), params);
	}
	
	
	
	
	/**
	 * 海关订单重推
	 * @param orderNumber
	 */
	public void updateHGRestart(String orderNumber) {
		StringBuffer 	hql = new StringBuffer("update KllOrderSynApi t set t.sendHg=0,t.sendHgDate=? where t.orderNumber=? ");
		Object[] params = {new Timestamp(System.currentTimeMillis()),orderNumber};
		this.executeHql(hql.toString(), params);
	}
	
	/**
	 * 取消卓志订单
	 */
	public void updateSynByZZ(String orderNumber) {
		StringBuffer 	hql = new StringBuffer("update KllOrderSynApi t set t.sendZz=4,t.sendZzDate=?,t.zt=2,t.synDate=? where t.orderNumber=? ");
		Object[] params = {new Timestamp(System.currentTimeMillis()),new Timestamp(System.currentTimeMillis()),orderNumber};
		this.executeHql(hql.toString(), params);
	}
	
	
	/**
	 * 取消EDI订单
	 */
	public void cancelEDIresult(String orderNumber) {
		StringBuffer 	hql = new StringBuffer("update KllOrderSynApi t set t.sendNr=4,t.sendNrDate=?,t.synDate=? where t.orderNumber=? ");
		Object[] params = {new Timestamp(System.currentTimeMillis()),new Timestamp(System.currentTimeMillis()),orderNumber};
		this.executeHql(hql.toString(), params);
	}
	
	/**
	 * 卓志订单异常
	 */
	public void updateZZStatus(String ibilling_number) {
		StringBuffer 	hql = new StringBuffer("update KllOrderSynApi t set t.sendZz=5,t.sendZzDate=? where t.ibillingNumber=?  ");
		Object[] params = {new Timestamp(System.currentTimeMillis()),ibilling_number};
		this.executeHql(hql.toString(), params);
	}
	
	
	/**
	 * 获取未发货订单
	 */
	public List<KllOrderSynApi> getLogisticExmail() {
		StringBuffer 	hql = new StringBuffer("SELECT * FROM kll_order_syn_api a WHERE a.send_nr_date < DATE_SUB(NOW(),INTERVAL 1 DAY) AND a.send_nr=2 or a.send_nr =3 ");
		Map<String,Object> params=new HashMap<String, Object>();
		List<KllOrderSynApi> list =  excuteSql(hql.toString());
		return list;
	}
}
